﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KataBankOCR
{
    class Program
    {
        /// <summary>
        /// Code to do user story 1, 3 and 4 output, plus a multiple error correction case
        /// </summary>
        /// <param name="args">No command line args needed</param>
        static void Main(string[] args)
        {
            const string dividingLine = "________________________________________________________________________________";
            const string filenameIndent = "   ";
            
            // user story 1
            Console.WriteLine(dividingLine);
            Console.WriteLine("KataBankOCR written by Paul S. Manning, Jr.");
            Console.WriteLine(dividingLine);
            Console.WriteLine("User Story 1 output reading:");
            ScannedFile ocrFile = new ScannedFile();
            string path = AppDomain.CurrentDomain.BaseDirectory;

            string fileToRead = path + "..\\..\\Test Data\\useCase1SuggestedInput.txt";
            Console.WriteLine(filenameIndent + fileToRead);
            string[] digitString = ocrFile.firstAccountNumber(fileToRead);
            while (digitString != null)
            {
                AccountNumber accNum = new AccountNumber(digitString,AccountNumber.noErrorCorrection);
                Console.WriteLine(accNum.ToString());
                digitString = ocrFile.nextAccountNumber();
            }

            // user story 3
            fileToRead = path + "..\\..\\Test Data\\useCase3SuggestedInput.txt";
            string fileToWrite = path + "useCase3Output.txt";
            Console.WriteLine(dividingLine);
            Console.WriteLine("User Story 3 reading:");
            Console.WriteLine(filenameIndent + fileToRead);
            Console.WriteLine("Output in:");
            Console.WriteLine(filenameIndent + fileToWrite);
            using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(@fileToWrite))
            {
                digitString = ocrFile.firstAccountNumber(fileToRead);
                while (digitString != null)
                {
                    AccountNumber accNum = new AccountNumber(digitString,AccountNumber.noErrorCorrection);
                    outfile.WriteLine(accNum.ToString() + " " + accNum.Status);
                    digitString = ocrFile.nextAccountNumber();
                }
            }
            

            // user story 4
            Console.WriteLine(dividingLine);
            Console.WriteLine("User Story 4 output reading:");
            fileToRead = path + "..\\..\\Test Data\\useCase4SuggestedInput.txt";
            Console.WriteLine(filenameIndent + fileToRead);
            digitString = ocrFile.firstAccountNumber(fileToRead);
            while (digitString != null)
            {
                AccountNumber accNum = new AccountNumber(digitString,AccountNumber.oneDigitErrorCorrection);
                Console.WriteLine(accNum.ToString());
                digitString = ocrFile.nextAccountNumber();
            }

            // multiple digit correction output
            Console.WriteLine(dividingLine);
            Console.WriteLine("Multiple digit correction output with User Story 4 input reading:");
            fileToRead = path + "..\\..\\Test Data\\useCase4SuggestedInput.txt";
            Console.WriteLine(filenameIndent + fileToRead);
            digitString = ocrFile.firstAccountNumber(fileToRead);
            while (digitString != null)
            {
                AccountNumber accNum = new AccountNumber(digitString, AccountNumber.multiDigitErrorCorrection);
                Console.Write(accNum.DigitString + " " + accNum.Status + " " +
                    accNum.OtherPossibleAccountNumbers.Count.ToString() + " valid account number variations");
                if (accNum.OtherPossibleAccountNumbers.Count > 0)
                {
                    Console.Write(" [" + accNum.OtherPossibleAccountNumbers[0]);
                    if (accNum.OtherPossibleAccountNumbers.Count > 1)
                    {
                        Console.Write(" ... " + accNum.OtherPossibleAccountNumbers[accNum.OtherPossibleAccountNumbers.Count - 1] + "");
                    }
                    Console.Write("]");
                }
                Console.WriteLine();
                digitString = ocrFile.nextAccountNumber();
            }

        }
    }
}

